package com.d3works.apps.base.service.batch;

import com.d3works.apps.base.db.Batch;
import com.d3works.apps.base.db.ImportBatch;
import com.d3works.apps.base.db.repo.ImportBatchRepository;
import com.d3works.apps.base.exceptions.IExceptionMessage;
import com.d3works.apps.base.service.administration.AbstractBatchService;
import com.d3works.db.Model;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;

public class ImportBatchService extends AbstractBatchService {

  @Override
  protected Class<? extends Model> getModelClass() {
    return ImportBatch.class;
  }

  @Override
  public Batch run(Model model) throws D3worksException {

    Batch batch;
    ImportBatch importBatch = (ImportBatch) model;

    switch (importBatch.getActionSelect()) {
      case ImportBatchRepository.ACTION_SELECT_IMPORT:
        batch = importData(importBatch);
        break;
      case ImportBatchRepository.ACTION_SELECT_ADVANCE_IMPORT:
        batch = advancedImportData(importBatch);
        break;
      default:
        throw new D3worksException(
            TraceBackRepository.CATEGORY_INCONSISTENCY,
            I18n.get(IExceptionMessage.BASE_BATCH_1),
            importBatch.getActionSelect(),
            importBatch.getCode());
    }

    return batch;
  }

  public Batch importData(ImportBatch importBatch) {
    return Beans.get(BatchImportData.class).run(importBatch);
  }

  public Batch advancedImportData(ImportBatch importBatch) {
    return Beans.get(BatchAdvancedImportData.class).run(importBatch);
  }
}
